$.ajax({
    type:'get',
    url:'/mywork.json/',
    dataType:'json',
    success:function(data) {
        $('#username').text(data.username);
        $('#profile').prop('src', data.profile);
    }
});
$('#logout').click(function () {
    let t = new Date();
    t.setTime(t.getTime() - 1);
    document.cookie = 'username=-1;expires=' + t.toUTCString();
    document.cookie = 'password=-1;expires=' + t.toUTCString();
});
let AIclass;
$.ajax({
    type:'get',
    url:'/static/js/AIclassification.json',
    dataType:'json',
    success:function(data){
        AIclass = data;
        for (let j in data)
            $('#field').append('<option>' + data[j].name + '</option>');
        for (let j in data[0].list)
            $('#task').append('<option>' + data[0].list[j].name + '</option>');
        for (let j in data[0].list[0].list)
            $('#subtask').append('<option>' + data[0].list[0].list[j].name + '</option>');
        for (let j in data[0].list[0].list[0].list)
            $('#benchmark').append('<option>' + data[0].list[0].list[0].list[j] + '</option>');
    }
});
let select4 = ['field', 'task', 'subtask', 'benchmark'];
let chosen = new Set();
$('#choose').click(function () {
    let s = '';
    for (let j in select4) {
        s += $('#' + select4[j]).find('option:selected').text();
        if (j != 3) s += '>';
    }
    if (! chosen.has(s)) {
        $('#chosen').append(`<option data-toggle="tooltip" title="${s}">${s}</option>`);
        chosen.add(s);
    }
});
$('#remove').click(function () {
    $('#chosen').find('option:selected').each(function () {
        chosen.delete($(this).text());
        $(this).remove();
    });
});
$('#field').change(function () {
    let ind0 = $('#field')[0].selectedIndex;
    $('#task').empty();
    $('#subtask').empty();
    $('#benchmark').empty();
    for (let j in AIclass[ind0].list)
        $('#task').append('<option>' + AIclass[ind0].list[j].name + '</option>');
    for (let j in AIclass[ind0].list[0].list)
        $('#subtask').append('<option>' + AIclass[ind0].list[0].list[j].name + '</option>');
    for (let j in AIclass[ind0].list[0].list[0].list)
        $('#benchmark').append('<option>' + AIclass[ind0].list[0].list[0].list[j] + '</option>');
});
$('#task').change(function () {
    let ind0 = $('#field')[0].selectedIndex;
    let ind1 = $('#task')[0].selectedIndex;
    $('#subtask').empty();
    $('#benchmark').empty();
    for (let j in AIclass[ind0].list[ind1].list)
        $('#subtask').append('<option>' + AIclass[ind0].list[ind1].list[j].name + '</option>');
    for (let j in AIclass[ind0].list[ind1].list[0].list)
        $('#benchmark').append('<option>' + AIclass[ind0].list[ind1].list[0].list[j] + '</option>');
});
$('#subtask').change(function () {
    let ind0 = $('#field')[0].selectedIndex;
    let ind1 = $('#task')[0].selectedIndex;
    let ind2 = $('#subtask')[0].selectedIndex;
    $('#benchmark').empty();
    for (let j in AIclass[ind0].list[ind1].list[ind2].list)
        $('#benchmark').append('<option>' + AIclass[ind0].list[ind1].list[ind2].list[j] + '</option>');
});
$('#create').click(function () {
    let workname = $('#workname').val();
    if (workname == '') { alert('Work name is empty.'); return; }
    let description = $('#description').val();
    if (description == '') { alert('Description is empty.'); return; }
    let chosens = '';
    chosen.forEach((x) => {
        chosens += x + '@';
    });
    if (chosens == '') { alert("At least choose 1 field"); return; }
    $.ajax({
        type: 'post',
        url: '/create/',
        data: {
            workname: workname,
            description: description,
            chosens: chosens
        },
        success: function(result, t) {
            if (result.length == 32) location.href = '/detail.htm?id=' + result;
            else alert(result);
        },
        error: function (http, e) {
            alert(http.status + e);
        }
    });
});
